namespace PHSRAG.Insight21.FormsManagement.Controls
{
	using System;
	using System.Text;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.ComponentModel;

	/// <summary>
	///	CustomValueSelector is the UI displayed to the user when a CustomValueSelector is rendered
	/// </summary>
	public partial class Migrated_CustomValueSelector : CustomValueSelector
	{
		private string parentName;
		private string parentValue;
		private string itemValues;
		private string selectedListItemValue;
		private string pageUrl;

		/// <summary>
		/// Gets or sets the parent question name for the CustomValueSelector question
		/// </summary>
		override public string ParentName
		{
			get { return parentName; }
			set { parentName = value; }
		}

		/// <summary>
		/// Gets or sets the parent value for the CustomValueSelector question
		/// </summary>
		override public string ParentValue
		{
			get { return parentValue; }
			set { parentValue = value; }
		}

		/// <summary>
		/// Gets or sets the '^' delimited list of values for the listbox in CustomValueSelector control
		/// </summary>
		override public string ItemValues
		{
			get 
			{
				int itemCount = itemsList.Items.Count;
				StringBuilder items = new StringBuilder();
				for (int i=0; i<itemCount; i++)
				{
					items.Append(string.Format("{0}^", itemsList.Items[i].Value));
				}
				string allItems = items.ToString();
				if (allItems.Length > 0)
					allItems = allItems.Substring(1, allItems.Length - 1);
				return allItems;
			}
			set
			{
				itemValues = value;
				
			}
		}

		/// <summary>
		/// Gets or sets the selected item in the list box
		/// </summary>
		override public string SelectedListItemValue
		{
			get
			{
				return (itemsList.SelectedIndex >= 0) ? itemsList.SelectedItem.Value : string.Empty;
			}
			set
			{
				selectedListItemValue = value;
			}
		}

		/// <summary>
		/// Gets or sets the url of the custom value selection page
		/// </summary>
		public string PageUrl
		{
			get { return pageUrl; }
			set { pageUrl = value; }
		}

		/// <summary>
		/// Loads options already entered by the user, if any, into the list box. Each option has an associated text that is visible to the user and value that uniquely 
		/// identifies the option.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void Page_Load(object sender, System.EventArgs e)
		{
			//javascript to open the value selection page as a modal dialog

			addItem.Attributes.Add("onclick", "javascript:return ShowSelectionPage('" + pageUrl + "', '" + newItemText.ClientID + "', '" + newItemValue.ClientID + "');");


			itemsList.Items.Clear();
			string[] itemValueArray = itemValues.Split('^');
			int itemCount = itemValueArray.Length;
			for (int i = 0; i < itemCount; ++i)
			{
				if (itemValueArray[i] != string.Empty)
				{
					string[] pair = itemValueArray[i].Split('|');
					itemsList.Items.Add(new ListItem(pair[1], itemValueArray[i]));
					if (itemValueArray[i] == selectedListItemValue)
					{
						itemsList.SelectedIndex = itemsList.Items.Count - 1;
					}
				}
			}

			if (itemsList.Items.Count > 0)
			{
				removeItem.Visible = true;
			}

		}

		#region Web Form Designer generated code
		/// <summary>
		/// Page Initialization
		/// </summary>
		/// <param name="e">Arguments associated with the event.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			
		    base.OnInit(e);
		}
		#endregion

		/// <summary>
		/// If not already added, adds the new option entered to the list box. This option is also added to the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnAddItemClick(object sender, EventArgs e)
		{
			string itemText = newItemText.Value;
			string itemValue = newItemValue.Value;
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			if (itemValue != "" && itemsList.Items.FindByValue(itemValue) == null)
			{
				itemsList.Items.Add(new ListItem(itemText, itemValue));
				removeItem.Visible = true;
				viewer.AddMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), itemValue);
			}
			else
			{
				viewer.Refresh();
			}
		}

		/// <summary>
		/// Removes the selected option from the list box. This option is also deleted from the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnRemoveItemClick(object sender, EventArgs e)
		{
			int selectedIndex = itemsList.SelectedIndex;
			string removedItem = itemsList.SelectedItem.Value;
			if (selectedIndex >= 0)
				itemsList.Items.RemoveAt(selectedIndex);
			if (itemsList.Items.Count == 0)
			{
				removeItem.Visible = false;
			}
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			viewer.RemoveMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), removedItem);
		}

		/// <summary>
		/// Based on the option selected, the answers for the child questions, if any, are displayed.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnItemsListSelectedIndexChanged(object sender, System.EventArgs e)
		{
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			viewer.Refresh();
		}

	}
}
